Skip to content

DOC: slice_indexer correction + examples #17829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

topper-123
Copy link
Contributor

@topper-123 topper-123 commented Oct 9, 2017

The stated return value of Index.slice_indexer in the doc string is wrong, the method can only return slices. Also added some examples.

In addition, some very minor corrections on the user guide text on IntervalIndex (moved the versionadded to the chapter top + a spelling correction). EDIT: These are not related to the .slice_indexer changes, but are attached to this PR as the changes are very small.

@jreback jreback added Docs Indexing Related to indexing on series/frames, not to indexes themselves labels Oct 10, 2017
@topper-123 topper-123 force-pushed the MultiIndex.slice_indexer branch from ccc34b2 to bb8050a Compare October 10, 2017 07:40
@codecov
Copy link

codecov bot commented Oct 10, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@390f36e). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #17829   +/-   ##
=========================================
  Coverage          ?   91.22%           
=========================================
  Files             ?      163           
  Lines             ?    49994           
  Branches          ?        0           
=========================================
  Hits              ?    45606           
  Misses            ?     4388           
  Partials          ?        0
Flag Coverage Δ
#multiple 89.02% <ø> (?)
#single 40.24% <ø> (?)
Impacted Files Coverage Δ
pandas/core/indexes/base.py 96.47% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 390f36e...bb8050a. Read the comment docs.

@codecov
Copy link

codecov bot commented Oct 10, 2017

Codecov Report

Merging #17829 into master will decrease coverage by 0.04%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17829      +/-   ##
==========================================
- Coverage   91.26%   91.22%   -0.05%     
==========================================
  Files         163      163              
  Lines       50099    50099              
==========================================
- Hits        45724    45703      -21     
- Misses       4375     4396      +21
Flag Coverage Δ
#multiple 89.03% <ø> (-0.03%) ⬇️
#single 40.24% <ø> (-0.06%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/base.py 96.42% <ø> (ø) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/plotting/_converter.py 63.38% <0%> (-1.82%) ⬇️
pandas/core/frame.py 97.75% <0%> (-0.1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a355ed2...e78f3cd. Read the comment docs.

@@ -3412,7 +3412,7 @@ def _get_string_slice(self, key, use_lhs=True, use_rhs=True):

def slice_indexer(self, start=None, end=None, step=None, kind=None):
"""
For an ordered Index, compute the slice indexer for input labels and
For an ordered index, compute the slice indexer for input labels and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not fully sure it has to be ordered? When there are no duplicates, I think it doesn't need to be sorted:

In [9]: pd.Index([1, 3, 4, 2, 5]).slice_indexer(1, 2)
Out[9]: slice(0, 4, None)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can add a "Raises" section to specify when a KeyError is raised:

In [10]: pd.Index([1, 3, 4, 2, 5, 2]).slice_indexer(1, 2)
...
KeyError: 'Cannot get right slice bound for non-unique label: 2'

In [11]: pd.Index([1, 3, 4, 2, 5, 2]).slice_indexer(1, 4)
Out[11]: slice(0, 3, None)

@@ -3426,11 +3426,23 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):

Returns
-------
indexer : ndarray or slice
indexer : slice

Notes
-----
This function assumes that the data is sorted, so use at your own peril
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although there is this note .. Not fully sure what it means ..

@topper-123 topper-123 force-pushed the MultiIndex.slice_indexer branch from 0652130 to e78f3cd Compare October 29, 2017 23:06
@topper-123
Copy link
Contributor Author

Ok, I've changed according to the suggestions.

I think the warning is ok, because I'm a bit surprised that it's even possible to slice a non-ordered unique index; maybe you'd want that some times, but I bet 9 out of 10 times, someone who slices a index expects it to be ordered.

@jorisvandenbossche jorisvandenbossche merged commit 52fe6bc into pandas-dev:master Oct 30, 2017
@jorisvandenbossche
Copy link
Member

@topper-123 Thanks!

@jorisvandenbossche jorisvandenbossche added this to the 0.22.0 milestone Oct 30, 2017
peterpanmj pushed a commit to peterpanmj/pandas that referenced this pull request Oct 31, 2017
@topper-123 topper-123 deleted the MultiIndex.slice_indexer branch November 6, 2017 21:46
No-Stream pushed a commit to No-Stream/pandas that referenced this pull request Nov 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants